package com.gzpi.hwgggh.service.impl;

import com.gzpi.hwgggh.mapper.DWXMapper;
import com.gzpi.hwgggh.mapper.StreetMapper;
import com.gzpi.hwgggh.service.StreetService;
import com.gzpi.hwgggh.sysenum.EnumErrorCode;
import com.gzpi.hwgggh.utils.CommonTools;
import com.gzpi.hwgggh.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date : 2021-02-08 10:58
 **/
@Service
@Transactional
public class StreetServiceImpl implements StreetService {

    @Autowired
    private StreetMapper streetMapper;

    @Autowired
    private DWXMapper dwxMapper;

    @Override
    public Result selectdAllXzq() {
        try {
            List<String> list = streetMapper.selectdAllXzq();
            if (list!=null && list.size()>0){
                return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),list);
            }
            return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("行政区查询失败"));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }

    @Override
    public Result selectAllJDByXzq(String xzq) {
        try {
            if (StringUtils.isNotBlank(xzq)){
                List<String> list = streetMapper.selectAllJDByXzq(xzq);
                if (list!=null && list.size()>0){
                    return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),list);
                }
                return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("所属街道查询失败"));
            }
            return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("参数不能为空"));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }

    @Override
    public Result selectAllDwbhByXzqAndSzjd(String xzq, String szjd) {
        try {
            if (StringUtils.isNotBlank(xzq) || StringUtils.isNotBlank(szjd)){
                List<Map<String, Object>> list = dwxMapper.selectAllDwbhByXzqAndSzjd(xzq,szjd);

                Map<String,Object> map1 = new HashMap<>();

                for (Map<String, Object> map : list) {
                    String dwbh = map.get("XSDWBH").toString();
                    if (!map1.containsKey(dwbh)){
                        map1.put(dwbh,map);
                    }
                }

                Map<String, Object> map2 = CommonTools.sortMapByKey(map1);
                List<Map<String,Object>> list1 = new ArrayList<>();

                for (String key : map2.keySet()) {
                    list1.add((Map<String, Object>) map2.get(key));
                }


                if (list1!=null && list1.size()>0){
                    return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),list1);
                }
                return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("点位编号查询失败"));
            }
            return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("参数不能为空"));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }
}
